// 引入依赖
const express = require('express');
const noteService = require('./noteService');
const moment = require("moment");
const router = express.Router();


// 新增笔记
router.post('/savenote', (req, resp) => {
    console.log('请求体参数：', JSON.stringify(req.body));
    // 从请求体中获取笔记的主体内容，线索内容，总结内容
    const {mainContent} = req.body;
    if (!mainContent) {
        return resp.status(500)
            .json({
                code: 500,
                msg: '请输入笔记的主体内容',
            });
    }
    noteService.insertNote(req.body).then(r => {
    });
    resp.status(200)
        .json({
            code: 200,
            msg: '保存成功',
            data: null
        });
})

// 将笔记转换为VO, 并且将笔记按照更新时间降序排列
function convertNoteToVO(notes) {
    notes.forEach(note => {
            // 将时间戳转换为时间字符串
            note.createTime = moment(note.createTime).format('YYYY-MM-DD HH:mm:ss');
            note.updateTime = moment(note.updateTime).format('YYYY-MM-DD HH:mm:ss');
            note.id = note._id.toString();
            // 删除_id和__v字段
            delete note._id;
            delete note.__v;
        }
    );
    notes.sort((a, b) => {
        return b.updateTime - a.updateTime;
    });
    return notes;
}

// 获取所有笔记
router.get('/getAllNotes', async (req, resp) => {
    const notes = await noteService.getAllNotes();
    const noteVos = convertNoteToVO(notes);
    resp.status(200)
        .json({
            code: 200,
            msg: '成功',
            data: noteVos,
        });
})

// 分页获取笔记
router.post('/pageQueryNotes', async (req, resp) => {
    const {rawPage, rawLimit, keyword, createTimeStart,createTimeEnd,
        updateTimeStart,updateTimeEnd, mainContent, clueContent, summaryContent} = req.body;
    const page = parseInt(rawPage) || 1;
    const limit = parseInt(rawLimit) || 10;
    const condition = {
        page, limit,
        keyword, createTimeStart,createTimeEnd,
        updateTimeStart,updateTimeEnd, mainContent, clueContent, summaryContent
    };
    const notes = await noteService.pageQueryNotes(condition);
    const noteVos = convertNoteToVO(notes);
    resp.status(200)
        .json({
            code: 200,
            msg: '成功',
            data: noteVos,
        });
});

// 删除笔记
router.delete('/deleteNote/:id', (req, resp) => {
    const noteId = req.params.id;
    noteService.deleteNote(noteId);
    resp.status(200)
        .json({
            code: 200,
            msg: '删除成功',
            data: null
        })
})

// 更新笔记
router.put('/updateNote', (req, resp) => {
    console.log('请求体参数：', JSON.stringify(req.body));
    // 从请求体中获取笔记的主体内容，线索内容，总结内容
    const {id, mainContent} = req.body;
    if (!id || !mainContent) {
        return resp.status(500)
            .json({
                code: 500,
                msg: '请求参数有误',
            });
    }
    noteService.updateNote(req.body);
    resp.status(200)
        .json({
            code: 200,
            msg: '更新成功',
        });
})

router.get('/', (req, resp) => {
    throw new Error('测试未捕获的异常');
})


module.exports = router;